package com.emellend.api.datewidgets;

import java.util.Calendar;
import com.emellend.api.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
/**
 * 默认在顶部显示现在的日期加时间。
 * 当点击两个按钮时，分别弹出时间选择器和日期选择器，将选择的结果显示到顶部。
 * @author emellend
 *
 */
public class DateWidgets1 extends Activity implements OnClickListener {
	
	private TextView mDateDisplay;
	// date and time
    private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;
	
	static final int TIME_DIALOG_ID = 0;
	static final int DATE_DIALOG_ID = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.date_widgets_example_1);
		init();
	}

	private void init() {
		mDateDisplay = (TextView) this.findViewById(R.id.dateDisplay);
		this.findViewById(R.id.pickDate).setOnClickListener(this);
		this.findViewById(R.id.pickTime).setOnClickListener(this);
		
		Calendar calendar=Calendar.getInstance();
		mYear=calendar.get(Calendar.YEAR);
		mMonth=calendar.get(Calendar.MONTH);
		mDay = calendar.get(Calendar.DAY_OF_MONTH);
	    mHour = calendar.get(Calendar.HOUR_OF_DAY);
	    mMinute = calendar.get(Calendar.MINUTE);
	    
	    updateDisplay();
	}

	private void updateDisplay() {
		mDateDisplay.setText( 
				new StringBuilder().append(mYear).append("年").append("-").
				append(mMonth + 1).append("月").append("-").append(mDay).append("日").
				append(" ").append(pad(mHour)).append(":").append(pad(mMinute)));
	}

	private Object pad(int hour) {
		if (hour>=10) {
			return String.valueOf(hour);
		}else {
			return "0"+hour;
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pickDate:
			showDialog(DATE_DIALOG_ID);
			break;
		case R.id.pickTime:
			showDialog(TIME_DIALOG_ID);
			break;
		}
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		
		switch (id) {
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, timePickListener, mHour, mMinute, false);
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, datePickListener, mYear, mMonth, mDay);
		}
		return null;
	}
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		switch (id) {
		case TIME_DIALOG_ID:
			((TimePickerDialog)dialog).updateTime(mHour, mMinute);
			break;
		case DATE_DIALOG_ID:
			((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
			break;
		}
	}
	private OnDateSetListener datePickListener =new OnDateSetListener(){
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear=year;
			mMonth=monthOfYear;
			mDay=dayOfMonth;
			updateDisplay();
			
		}};
	private OnTimeSetListener timePickListener=new OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour=hourOfDay;
			mMinute=minute;
			updateDisplay();
		}
	};


}
